package com.ruoyi.pg.pensionProject.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.pg.pensionProject.vo.PgProjectVo;
import com.ruoyi.pg.pensionProject.service.IPgProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 养老项目Controller
 *
 * @author Ming
 * @date 2025-09-03
 */
@Api(tags = "养老项目Controller")
@RestController
@RequestMapping("/pensionProject/project")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class PgProjectController extends BaseController {

    private final IPgProjectService pgProjectService;

    @ApiOperation("查询养老项目列表")
    @GetMapping("/list")
    public TableDataInfo<PgProjectVo> list(PgProjectVo entity) {
        return pgProjectService.queryList(entity);
    }

    @ApiOperation("查询首页养老项目列表")
    @GetMapping("/listIndex")
    public AjaxResult listIndex(PgProjectVo entity) {
        List<Long> roleIds = new ArrayList<>();
        for (SysRole role : getLoginUser().getUser().getRoles()) {
            roleIds.add(role.getRoleId());
        }
        entity.setRoleId(roleIds);
        return AjaxResult.success(pgProjectService.queryAll(entity));
    }

    @ApiOperation("查询养老项目所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(PgProjectVo entity) {
        return AjaxResult.success("查询成功", pgProjectService.queryAll(entity));
    }

    @ApiOperation("导出养老项目列表")
    @Log(title = "养老项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PgProjectVo entity) {
        List<PgProjectVo> list = pgProjectService.queryAll(entity);
        ExcelUtil<PgProjectVo> util = new ExcelUtil<>(PgProjectVo.class);
        util.exportExcel(response, list, "养老项目数据");
    }

    @ApiOperation("获取养老项目详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success("查询成功", pgProjectService.queryById(id));
    }

    @ApiOperation("新增养老项目")
    @Log(title = "养老项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PgProjectVo entity) {
        return toAjax(pgProjectService.save(entity));
    }

    @ApiOperation("修改养老项目")
    @Log(title = "养老项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PgProjectVo entity) {
        return toAjax(pgProjectService.updateById(entity));
    }

    @ApiOperation("删除养老项目")
    @Log(title = "养老项目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(pgProjectService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
